import Vue from 'vue'
import Vuex from 'vuex' // 引入Vuex
Vue.use(Vuex) // 应用Vuex插件
const actions = {} // 准备actions——用于响应组件中的动作
const mutations = {
    setLoginFlag(state,val){
        state.loginFlag=true
    },
    setMenu(state,val){
        state.menuList=JSON.parse(val);
        localStorage.setItem("menuList",val)
    },
    getMenu(state){
        state.menuList=state.menuList||localStorage.getItem("menuList")
    },
    setToken(state,val){
        state.token=val
        localStorage.setItem("token",val)
    },
    removeToken(state){
        state.token=''
        localStorage.removeItem("token")
    },
    getToken(state){
        state.token=state.token||localStorage.getItem("token")
    },
    setTagList(state,val){
        if(val.path!=='/index'){
            const flag=state.tagList.indexOf(val)
            if(flag===-1){
                state.tagList.push(val)
            }
        }
    },
    moveTag(state,val){
        if(val.name!=='/index'){
            const flag=state.tagList.indexOf(item=>item.cnName===val.cnName)
            if(flag!==-1){
                state.tagList.splice(flag, 1)
            }
        }
    }
} // 准备mutations——用于操作数据（state）
const state = {
    token:'',
    menuList:[],
    loginFlag:false,
    tagList:[
        {
            path:'/index',
            name: 'index',
            cnName:'首页'
        }
    ]
} // 准备state——用于存储数据
// 创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state,
})
